import logging

from selenium import webdriver
from selenium.webdriver.common.keys import Keys
import time
from selenium.webdriver.common.by import By
from selenium.webdriver.common.action_chains import ActionChains
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC

mail_url = 'https://mail.alibaba-inc.com/alimail/'
mail_name = 'juntao.zhang.zjt'

login_url ='https://login-ca.alibaba-inc.com:443'

# 忽略邮件地址
ignore = {'flyingfox', 'amap-builder', 'DSNotify', '高德地图iOS机器人', '集团监控中心', 'dike监控', '王蕊(晋梦)'}

ignoreContents = {'版本质量日报'}

no_read_url = ''


def ignore_mails(driver,wait):

    # 因为页面是轮训的更新页面信息所以。我们需要防止异常的处理。
    while(True):
        try:
            global no_read_url
            time.sleep(10)
            if no_read_url != '' and no_read_url != driver.current_url:
                driver.get(no_read_url)
            driver.refresh()
            time.sleep(10)
            wait.until(EC.presence_of_element_located((By.XPATH, ".//div[@class='e_abs_horizon_full cblist_virtual_block_wrap']")))

            if no_read_url == '':
                no_read_url = driver.current_url
            print(no_read_url)

            ## 内容匹配
            print("处理邮件时间:", time.strftime("%Y-%m-%d %H:%M:%S", time.localtime()))

            cnt = 0

            while(True):

                mail_list = driver.find_elements_by_xpath("//div[@class='e_draggable maillist_item_a select_wrap']")

                if cnt >= len(mail_list):
                    break

                mail_item = mail_list[cnt]

                # from
                from_mail = mail_item.find_elements_by_tag_name("span")[3].text

                # title
                title_mail = mail_item.find_elements_by_tag_name("span")[4].text[5:]

                unread = mail_item.find_elements_by_class_name("ico_mail_unread")

                if cnt % 6 == 0 and cnt != 0:
                   ## 页面滑动

                   next_page(wait=wait,driver=driver)

                if (contain_title(title_mail) or from_mail in ignore) and len(unread) > 0:
                    print(title_mail, from_mail, "click")

                    unread[0].click()
                    time.sleep(2)

                cnt += 1

        except Exception as e:
            logging.exception(e)


def contain_title(span):
    for ignoreContent in ignoreContents:
        if span.count(ignoreContent) > 0:
            return True

    return False


def handle_unread_mail():

    driver = webdriver.Chrome()
    try:
        wait = WebDriverWait(driver, 10)

        driver.get(mail_url)

        wait.until(EC.title_contains("阿里邮箱"))

        # 输出当前浏览器的标题
        print(driver.title)

        rs = wait.until(EC.presence_of_element_located((By.XPATH, ".//div[@class='e_tn_children_wrap']")))

        time.sleep(2)
        rs.find_elements_by_xpath(".//div[@class='e_tn_wrap']")[4].click()

        wait.until(EC.presence_of_element_located((By.XPATH, ".//div[@class='e_abs_horizon_full cblist_virtual_block_wrap']")))


        ignore_mails(driver,wait=wait)

    except Exception as ve:
        logging.exception(ve)

    driver.close()
    driver.quit()


def next_page(wait,driver):
    wait.until(EC.presence_of_element_located((By.XPATH,"//div[@class='e_abs_full cblist_virtual_panel_wrap']")))

    mail_windows = driver.find_element_by_xpath("//div[@class='e_abs_full cblist_virtual_panel_wrap']")

    print(mail_windows.get_attribute("id"))
    js = 'document.getElementById("%s").scrollTop=1000'%(mail_windows.get_attribute("id"))
    print(js)
    driver.execute_script(js)
    time.sleep(5)




if __name__ == '__main__':
    handle_unread_mail()